'use strict';

/**
 * @ngdoc function
 * @name payPcApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the payPcApp
 */
angular.module('payPcApp')
    .controller('mylibCtrl', function($scope, user_model, departmentmodel, AppConfig) {
        departmentmodel.getPrivate().success(function(resp) {
            console.log(resp);
            $scope.dep_list = resp.list;
        })
        $scope.get_user_info = function() {
            user_model.get_userinfo().success(function(resp) {
                console.log(resp);
                if (resp.statusCode == 0) {
                    $scope.user = resp.user;
                }
            });
        };
        $scope.get_user_info();

        $scope.show_userinfo = function() {
            $scope.edit_info = {};
            $scope.edit_info.email = $scope.user.email;
            $scope.edit_info.phone = $scope.user.phone;
            $scope.edit_info.mobile_phone = $scope.user.mobile_phone;
            $scope.edit_info.qq = $scope.user.qq;
            $scope.edit_info.address = $scope.user.address;
            $('#userinfomodal').modal();
        }
        $scope.get_lend_history_now = function(currentPage) {
            user_model.lend_history({
                currentPage: currentPage ? currentPage : 1,
                type: 1
            }).success(function(resp) {
                console.log(resp);
                if (resp.statusCode == 0) {
                    $scope.lend_history_now = resp.list;
                }
            })
        };
        $scope.get_lend_history_now(1);
        $scope.get_lend_history = function(currentPage) {
            user_model.lend_history({
                currentPage: currentPage ? currentPage : 1,
                type: 0
            }).success(function(resp) {
                console.log(resp);
                if (resp.statusCode == 0) {
                    $scope.lend_history = resp.list;
                }
            })
        };
        $scope.get_lend_history(1);
        $scope.get_myBookShelf = function(currentPage) {
            user_model.myBookShelf({
                currentPage: currentPage ? currentPage : 1
            }).success(function(resp) {
                console.log(resp);
                $scope.myBookShelf_list = resp.list;
            });
        };
        $scope.get_myBookShelf(1);
        $scope.get_myLoan = function(currentPage) {
            user_model.myLoan({
                currentPage: currentPage ? currentPage : 1
            }).success(function(resp) {
                console.log('123');
                console.log(resp);
                $scope.myLoan_list = resp.list;
            });
        };
        $scope.get_myLoan(1);

        $scope.unCollectABook = function(book_isbn) {
            user_model.unCollectABook({
                isbn: book_isbn
            }).success(function(resp) {
                if (resp.statusCode == 0) {
                    swal("提示!", "取消收藏成功!", "success");
                } else {
                    swal("提示!", "取消收藏失败!", "warning");
                }
                $scope.get_myBookShelf(1);
            });
        };
        $scope.save_info = function() {
            $('#edit_info_form').submit(function(e) {
                var data, xhr;
                data = new FormData();
                data.append('email', $scope.edit_info.email);
                data.append('mobile_phone', $scope.edit_info.mobile_phone);
                data.append('phone', $scope.edit_info.phone);
                data.append('qq', $scope.edit_info.qq);
                data.append('address', $scope.edit_info.address);
                console.log(data);

                xhr = new XMLHttpRequest();
                var url = AppConfig.WEB_ROOT + 'user/updateAUser/' + AppConfig.get_login_identity();
                console.log(url);
                xhr.open('POST', url, true);
                xhr.onreadystatechange = function(response) {
                    console.log(response);
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        var result = JSON.parse(xhr.responseText)
                        if (result.statusCode == 0) {
                            swal("提示!", "保存信息成功!", "success");
                            $('#userinfomodal').modal('hide');
                            $scope.get_user_info();
                        } else {
                            swal("提示!", "保存信息失败!", "warning");
                        }
                    }
                };
                xhr.send(data);
                console.log(data);
                e.preventDefault();
            });
        }

    });